﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FinanceGoldPointDal.DTO;
using System.Data;

namespace FinanceGoldPointDal.DAL
{
  public class Met : baseDAL
  {

    public void getSymbolsDataFromDB()
    {


    }

    public int UpdateSymbolsDAtaFromDB(SymbolDTO[] pSimboli)
    {
      Simboli simbolo = null;
      int conta = 0;

      using (var dal = getNewDAL())
      {
       // dal.Simboli.MergeOption = System.Data.Objects.MergeOption.NoTracking;

        foreach (var item in pSimboli)
        {

          simbolo = new Simboli()
          {
            IDsimbolo=-1,
            Code = item.Code,
            Name = item.Name,
            Isin = item.Isin,
            LastPrice = item.LastChangePrice,
            Location = item.Location,
            Types = item.Types,
            Attivo = true,
            Storicizza = true
          };

          try
          {
            ++conta;
            dal.Simboli.AddObject(simbolo);
            dal.SaveChanges(System.Data.Objects.SaveOptions.None);

            myDAL.AcceptAllChanges();


          }
          catch (Exception ex)
          {
            //var err = ex.Message;
            --conta;
            var er = ex.InnerException;
          }


        }

      }

        return conta;
      
    }


    public int UpdateSymbolInDB(SymbolDTO pItem)
    {
      Simboli simbolo = null;
      int conta = 0;

      using (var dal = getNewDAL())
      {
        // dal.Simboli.MergeOption = System.Data.Objects.MergeOption.NoTracking;

          simbolo = new Simboli()
          {
            IDsimbolo = -1,
            Code = pItem.Code,
            Name = pItem.Name,
            Isin = pItem.Isin,
            LastPrice = pItem.LastChangePrice,
            Location = pItem.Location,
            Types = pItem.Types,
            //YaHooLink = pItem.YHOOLink,
            Attivo = true,
            DateUpdateted = DateTime.Now ,
            Storicizza = true
          };

          try
          {
            ++conta;
            dal.Simboli.AddObject(simbolo);
            dal.SaveChanges(System.Data.Objects.SaveOptions.None);

            myDAL.AcceptAllChanges();


          }
          catch (Exception ex)
          {
            //var err = ex.Message;
            --conta;
            var er = ex.InnerException;
          }


        }

      

      return conta;

    }




  }
}
